<?php 
/**
 * 通过传递过来的sn号或者openid获取对应的clientId和用户信息，进行传递
 */
header("content-type:text/html;charset=utf8");
$openid = isset($_GET['openid'])?$_GET['openid']:'';
$device_sn = isset($_GET['device_sn'])?$_GET['device_sn']:'';
$pdo = new PDO('mysql:host=123.57.16.97;dbname=device_binding','root','zqVnUy6cxsQXbpEZ');
$pdo->exec('set names utf8');
if ($openid) {
	$clientid = adopt_openid_get_devicesn($pdo,$openid);
	$userinfo = adopt_openid_get_userinfo($openid);
	$push_data = array();
	$push_data['status'] = "1";
	$push_data['type'] = "login";
	$push_data['data'] = array("head_img_url"=>$userinfo['headimgurl'],"nickname"=>$userinfo['nickname']);
	$push_json_data['content'] = json_encode($push_data);
	$push_json_data['cid'] = $clientid;
}else if($device_sn){
	$clientid = adopt_devicesn_get_clientid($pdo,$device_sn);
	$openid = adopt_devicesn_get_openid($pdo,$device_sn);
	$userinfo = adopt_openid_get_userinfo($openid);
	$push_data = array();
	$push_data['status'] = "1";
	$push_data['type'] = "login";
	$push_data['data'] = array("head_img_url"=>$userinfo['headimgurl'],"nickname"=>$userinfo['nickname']);
	$push_json_data['content'] = json_encode($push_data);
 	$push_json_data['cid'] = $clientid;
}else{
	$push_data = array();
	$push_data['status'] = "0";
	$push_data['type'] = "login";
	$push_data['data'] = array('未携带有效参数');
	$push_json_data['content'] = json_encode($push_data);
}

curl_post_request($push_json_data);

//通过curl模拟post请求个推推送用户信息
function curl_post_request($post_data){
	$curl = curl_init();
	curl_setopt($curl, CURLOPT_URL, 'https://pay.hispot.cn/gitscan/push/getui.php');
	curl_setopt($curl, CURLOPT_HEADER, 1);
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_POST, 1);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
    $data = curl_exec($curl);
    curl_close($curl);
     print_r($data);
}
//通过openid来获取对应的设备(sn)号
function adopt_openid_get_devicesn($pdo,$openid){
	$sql = "SELECT * FROM binding WHERE openid = '$openid' ORDER BY `time` DESC LIMIT 1";
	$binding_data = $pdo->query($sql)->fetch(PDO::FETCH_ASSOC);
	return adopt_devicesn_get_clientid($pdo,$binding_data['device_sn']);
}
//通过设备号(sn)获取个推clientid
function adopt_devicesn_get_clientid($pdo,$device_sn){
	$sql = "SELECT * FROM relation WHERE device_sn = '$device_sn'";
	$relation_data = $pdo->query($sql)->fetch(PDO::FETCH_ASSOC);
	$clientid = $relation_data['clientid'];
	return $clientid;
}
//通过设备号(sn)获取openid
function adopt_devicesn_get_openid($pdo,$device_sn){
	$sql = "SELECT * FROM binding WHERE device_sn = '$device_sn'";
	$openid_data = $pdo->query($sql)->fetch(PDO::FETCH_ASSOC);
	return $openid_data['openid'];
}
//通过openid获取此openid所属的用户信息
function adopt_openid_get_userinfo($openid){
	$access_token = voucher_token();
	$url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$access_token."&openid=".$openid."&lang=zh_CN";
    return json_decode(file_get_contents($url),true);
}
//获取微信接口调用凭证access_token
function voucher_token(){  
    $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx2de38da107527240&secret=446fdd6335e31f0ceeaf63562ff03a4c";
    $access_token = json_decode(file_get_contents($url),true); 
    return $access_token['access_token'];
}


 ?>
